#include "../../mqserver/consumer.hpp"
#include <gtest/gtest.h>

MyRabbitMQ::ConsumerManager::ptr consumers;

void func1(const std::string& tag, const protoMQ::BaseProperties* base,const std::string& body)
{
    std::cout << "消费者" << tag << "消费了消息:" << body << std::endl; 
}

class ConsumerTest : public testing::Environment
{
    virtual void SetUp() override
    {
        consumers = std::make_shared<MyRabbitMQ::ConsumerManager>();
    }

    virtual void TearDown() override
    {

    }
} ;

TEST(ConsumerTest,insert_test)
{
    //添加队列管理单元
    consumers->declareQueueConsumer("queue-1");
    consumers->declareQueueConsumer("queue-2");
    consumers->declareQueueConsumer("queue-3");
    consumers->declareQueueConsumer("queue-4");
    consumers->declareQueueConsumer("queue-5");
    consumers->declareQueueConsumer("queue-6");


    //向队列管理单元添加消费者
    consumers->insertConsumer("consumers-1","queue-1",true,func1);
    consumers->insertConsumer("consumers-2","queue-1",true,func1);
    consumers->insertConsumer("consumers-3","queue-1",true,func1);
    consumers->insertConsumer("consumers-4","queue-1",true,func1);
    consumers->insertConsumer("consumers-5","queue-2",true,func1);
    consumers->insertConsumer("consumers-6","queue-2",true,func1);

    ASSERT_EQ(consumers->exists("queue-1","consumers-1"),true);
    ASSERT_EQ(consumers->exists("queue-1","consumers-2"),true);
    ASSERT_EQ(consumers->exists("queue-1","consumers-3"),true);
    ASSERT_EQ(consumers->exists("queue-1","consumers-4"),true);
    ASSERT_EQ(consumers->exists("queue-1","consumers-5"),false);

    ASSERT_EQ(consumers->exists("queue-2","consumers-5"),true);
    ASSERT_EQ(consumers->exists("queue-2","consumers-6"),true);

}

TEST(ConsumerTest,delete_test)
{
    consumers->deleteQueueConsumer("queue-3");
    consumers->deleteQueueConsumer("queue-300");

    consumers->deleteConsumer("queue-4","consumers-5");
    consumers->deleteConsumer("queue-2","consumers-5");
    consumers->deleteConsumer("queue-2","consumers-6");   
}

TEST(ConsumerTest,get_test)
{
    for(int i = 0; i < 10;++i)
    {
        MyRabbitMQ::Consumer::ptr it = consumers->getConsumer("queue-1");   
        //打印it的信息
        LOG(logLevel::DEBUG) << "消费者名称:" << it->m_name << std::endl;
    }

    MyRabbitMQ::Consumer::ptr it = consumers->getConsumer("queue-2");
    
    // LOG(logLevel::normal) << it->m_name << std::endl;
    // LOG(logLevel::normal) << it->m_autoAck << std::endl;
    // LOG(logLevel::normal) << it->m_subscriptionQueue << std::endl;
    
    ASSERT_EQ(it,MyRabbitMQ::Consumer::ptr());

}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    int ret = RUN_ALL_TESTS(); 
    return 0;
}